package uk.co.lunagroup.xmloperations;

import java.io.File;
import java.util.Vector;

import org.apache.poi.ss.usermodel.Row;

import uk.co.lunagroup.files.FileOperations;

/**
 * . Class required for creating BOM files for reporting purposes
 * 
 * @author robert.kowalski
 * 
 */
public class CreateBomFiles {

	/**
	 * @param args
	 */
	public static void main(final String[] args) {
		// choose file
		File fXmlFile = FileOperations.pickXMLFile();

		// build list of files to process
		Vector<FileList> fileRegister = ReadXMLFile
				.readXMLandReturnVector(fXmlFile);

		// read xml file with path, names and revisions and generate xml file
		for (int i = 0; i < fileRegister.size(); i++) {

			if (fileRegister.get(i).getXlsFile() != null) {
				createBom(fileRegister, i);
			}

		}
		// here should write fileRegister Vector to xml file
		WriteBomFile.updateFilesListXML(fileRegister, fXmlFile);

	}

	public static void createBom(final Vector<FileList> fileRegister,
			final int i) {

		File file = fileRegister.get(i).getXlsFile();

		// write rows to Vector rows. Opening file in sheet BOM
		Vector<Row> rows = ExcelOperations.readRows(ExcelOperations.openSheet(
				ExcelOperations.openExcelFile(file), "BOM"));

		String fileName = fileRegister.get(i).getTender() + " rev "
				+ fileRegister.get(i).getRevision() + " "
				+ fileRegister.get(i).getTenderName() + ".xml";

		// localPath - for Windows machine
		String filePath = file.getParent() + "\\" + fileName;

		// serverPath
		String serverPath = "/var/lib/tomcat7/webapps/birt/data/" + fileName;

		// TODO add condition for checking if excel modify date is different
		File saveFile = new File(filePath);
		WriteBomFile.writeBom(rows, saveFile);
		// and at the end adding information about filepath to
		// Now adding to fileRegister Vector. at the end of loop Vector
		// will save data to xml file
		fileRegister.get(i).setXmlFile(new File(filePath));
		fileRegister.get(i).setServerXmlFilePath(serverPath);

		// here add procedure to write fileinfo to the created file
		AddInfoToXMLFile.addInfo(saveFile, fileRegister.get(i));
	}
}
